import Promise from '../../../../utils/es6-promise/es6-promise'
import regeneratorRuntime from '../../../../utils/regenerator-runtime/runtime'
const utils = require('../../../../utils/util.js');
const app=getApp();
Page({
  data: {
    isHide:false,
    loadUrl:"/API/Card/cardList", //加载url
    page:1,
    pagesize:6,
    list:null,
    token:null,
    noMoreData:false,
    loadingText:"上拉加载更多",
    loadingTextArr:["上拉加载更多","加载中...","没有更多数据了"],
    delUrl:"/API/Card/del"
  },
  jumpEdit:function(e){
    var id=e.target.dataset.id;
    var url=id?'../edit/index?id='+id:'../edit/index';
    wx.navigateTo({
      url: url
    })
  },
  delCard:function(e){
    var id=e.target.dataset.id;
    var that=this;
    wx.showModal({
      title: '删除图片',
      content: '确定要删除该卡片？',
      showCancel: true,//是否显示取消按钮
      cancelText:"否",//默认是“取消”
      cancelColor:'skyblue',//取消文字的颜色
      confirmText:"是",//默认是“确定”
      confirmColor: 'skyblue',//确定文字的颜色
      success: function (res) {
         if (res.cancel) {
            //点击取消,默认隐藏弹框
         } else {
          that._delCard(id);
         }
      },
      fail: function (res) { },//接口调用失败的回调函数
      complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
   })
  },
  _delCard: async function(id){
    const data = await this._delCardRequest(id);
    if(data.state){
      wx.showToast({
        title:data.msg,
        icon:"none",
        duration:1000
      })
      this.setData({
        page:1,
        list:null
      })
      this.onLoad();
    }
    else{
      wx.showToast({
        title:data.msg,
        icon:"none",
        duration:1000
      })
    }
  },
  _delCardRequest:function(id){
      var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        app.request(this.data.delUrl,{token:this.data.token,id:id},res=>{
          //console.log(res);
          var data=res.data;
          if(data.state){
            //resolve({state:true,list:JSON.parse(data.list)});
            resolve({state:true,msg:"删除成功"});
          }
          else{
            resolve({state:false,msg:data.msg});          
          }
        },error=>{
          resolve({state:false,msg:"删除失败，请重试"});
        })
    });
    return p;
  },
  jumpNew:function(e){
    var url='../new/index';
    wx.navigateTo({
      url: url
    })
  },
  onShow:function(){
    console.log("onShow");
    if(this.data.isHide) this.onLoad();
    this.setData({
      isHide:false
    })
  },
  onHide:function(){
    this.setData({
      isHide:true
    })
  },
  onLoad: async function () {
    console.log("onLoad");
    const token = await app.getToken();
    this.setData({
      token:token,
      loadingText:this.data.loadingTextArr[1],
      page:1
    })
    var result=await this._loadData();
    if(result.state){
      var list=result.list;
      this.setData({
        list:list,
        loadingText:list.length==0?this.data.loadingTextArr[2]:this.data.loadingTextArr[0],
        noMoreData:list.length==0
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
      this.setData({
        loadingText:this.data.loadingTextArr[0]
      })
    }
  },
  _loadData:function(){
      var p = new Promise((resolve,reject)=>{
          //做一些异步操作
          app.request(this.data.loadUrl,{token:this.data.token,page:this.data.page,pagesize:this.data.pagesize},res=>{
            //console.log(res);
            var data=res.data;
            /* var data={
              state:true,
              data:[
                {
                  u_id:1,
                  card_name:"白金卡",
                  bank_name:"招商银行",
                  limit:1222,
                  bill_day:3,
                  repay_day:5,
                  annual_fee_day:"7/13"
                },
                {
                  u_id:1,
                  card_name:"白金卡",
                  bank_name:"招商银行",
                  limit:1222,
                  bill_day:3,
                  repay_day:5,
                  annual_fee_day:"7/13"
                }
            ]
            } */
            if(data.state){
              //resolve({state:true,list:JSON.parse(data.list)});
              resolve({state:true,list:data.data});
            }
            else{
              resolve({state:false,msg:data.msg});          
            }
          },error=>{
            resolve({state:false,msg:"刷新失败，请重试"});
          })
      });
      return p;
  },
  onPullDownRefresh: async function () {
    this.setData({
      page:1
    })
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var result= await this._loadData();
    if(result.state){
      var list=result.list;
      this.setData({
        list:list,
        loadingText:list.length==0?this.data.loadingTextArr[2]:this.data.loadingTextArr[0],
        noMoreData:list.length==0
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
      this.setData({
        loadingText:this.data.loadingTextArr[0]
      })
    }
    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading();
    // 停止下拉动作
    wx.stopPullDownRefresh();
  },
  onReachBottom:  async function () {
    if(this.data.noMoreData) return;
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    this.setData({
      page:this.data.page+1,
      loadingText:this.data.loadingTextArr[1]
    })
    var result=await this._loadData();
    if(result.state){
      var list=this.data.list.concat(result.list);
      this.setData({
        list:list,
        loadingText:result.list.length==0?this.data.loadingTextArr[2]:this.data.loadingTextArr[0],
        noMoreData:result.list.length==0
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
      this.setData({
        loadingText:this.data.loadingTextArr[0]
      })
    }
    // 隐藏加载框
    wx.hideLoading();
  }
})
